const fs = require('fs');
const path = require('path');

class FileExplorer {
  static listFiles(dirPath) {
    try {
      const items = fs.readdirSync(dirPath, { withFileTypes: true });
      return items.map(item => ({
        name: item.name,
        type: item.isDirectory() ? 'directory' : 'file',
        size: item.isFile() ? fs.statSync(path.join(dirPath, item.name)).size : 0,
        modified: fs.statSync(path.join(dirPath, item.name)).mtime
      }));
    } catch (error) {
      throw new Error(`无法读取目录: ${error.message}`);
    }
  }

  static getFileContent(filePath) {
    try {
      return fs.readFileSync(filePath, 'utf-8');
    } catch (error) {
      throw new Error(`无法读取文件: ${error.message}`);
    }
  }
}

module.exports = FileExplorer;